package org.csu.jpetstore.service.impl;

import com.sun.istack.internal.NotNull;
import org.csu.jpetstore.bo.AccountBO;
import org.csu.jpetstore.entity.Account;
import org.csu.jpetstore.entity.Signon;
import org.csu.jpetstore.mapper.AccountMapper;
import org.csu.jpetstore.mapper.SignonMapper;
import org.csu.jpetstore.service.AccountService;
import org.csu.jpetstore.service.TokenService;
import org.csu.jpetstore.util.MyToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author huangzhangyan
 */
@Service
public class AccountServerImpl implements AccountService {
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private SignonMapper signonMapper;
    @Autowired
    private TokenService tokenService;


    /**
     * 通过用户名获取用户
     *
     * @param username 用户名
     * @return AccountBO
     * @author huangzhangyan
     * @date: 2022/4/3 22:27
     */
    @Override
    public AccountBO getAccount(@NotNull String username) {
        Account account = accountMapper.selectById(username);
        Signon signon = signonMapper.selectById(username);

        return AccountBO.toAccountBO(account, signon);
    }

    /**
     * 用户名是否存在,存在返回true,不存在返回false
     *
     * @param username 用户名
     * @return boolean
     * @author huangzhangyan
     * @date 2022/4/3 22:59
     */
    @Override
    public boolean isUsernameExist(@NotNull String username) {
        return (this.getAccount(username) != null);
    }

    /**
     * 登录(用户名和密码),登录成功返回MyTokenBO
     *
     * @param username 用户名
     * @param password 密码
     * @return MyToken 登录失败返回null
     * @author huangzhangyan
     * @date 2022/4/3 23:11
     */
    @Override
    public MyToken login(@NotNull String username, @NotNull String password) {
        //TODO
        AccountBO accountBO = getAccount(username);
        if (accountBO == null || !accountBO.getPassword().equals(password)) {
            return null;
        }
        return tokenService.createToken(username);
    }

//    /**
//     * 校验验证码
//     *
//     * @param codeKey   验证码的ID
//     * @param codeValue 验证码的值
//     * @return boolean
//     * @author huangzhangyan
//     * @date 2022/4/3 23:32
//     */
//    @Override
//    public boolean verifyCode(@NotNull String codeKey, @NotNull String codeValue) {
//        return CodeUtil.verifyCode(codeKey, codeValue);
//    }
//
//    /**
//     * 获取验证码ID和图片
//     *
//     * @return Pair<String, Graphics> 验证码ID(不是验证码的值),验证码的图片
//     * @author huangzhangyan
//     * @date 2022/4/4 1:25
//     */
//    @Override
//    public Pair<String, Graphics> getCodeKeyAndGraphics() {
//        return CodeUtil.getCodeKeyAndGraphics();
//    }

    /**
     * 新增用户
     *
     * @param accountBO 新增用户
     * @return boolean 用户名存在返回false
     * @author huangzhangyan
     * @date 2022/4/6 0:39
     */
    @Override
    public boolean insertAccount(AccountBO accountBO) {
        if (this.isUsernameExist(accountBO.getUsername())) {
            return false;
        }
        signonMapper.insert(AccountBO.toSignonEntity(accountBO));
        accountMapper.insert(AccountBO.toAccountEntity(accountBO));
        return true;
    }

    /**
     * 修改账户
     *
     * @param accountBO 修改的账户信息
     * @author huangzhangyan
     * @date 2022/4/12 9:00
     */
    @Override
    public void modifyAccount(AccountBO accountBO) {
        if (!this.isUsernameExist(accountBO.getUsername())) {
            throw new RuntimeException("不存在用户名为: " + accountBO.getUsername() + "的用户");
        }
        signonMapper.updateById(AccountBO.toSignonEntity(accountBO));
        accountMapper.updateById(AccountBO.toAccountEntity(accountBO));
    }
}